#!/bin/bash

# 定义程序路径和文件名的数组
declare -a APP_SERVER_PATHS=("/home/app/erp/")
declare -a APP_SERVERS=("app-2.1.7.jar")

# 停止并重启每个程序
for ((i = 0; i < ${#APP_SERVER_PATHS[@]}; i++)); do
  APP_SERVER_PATH=${APP_SERVER_PATHS[$i]}
  APP_SERVER=${APP_SERVERS[$i]}

  # 查找正在运行的Java进程的PID
  PID=$(ps aux | grep java | grep "$APP_SERVER" | grep -v grep | awk '{print $2}')

  if [ -z "$PID" ]; then
    echo "demo-test-1.0.0 server application $i is not running."
  else
    # 杀死进程
    echo "Stopping demo-test-1.0.0 server application $i (PID: $PID)..."
    kill $PID
    sleep 5
  fi
done

# 确保进程已停止后再启动程序
for ((i = 0; i < ${#APP_SERVER_PATHS[@]}; i++)); do
  APP_SERVER_PATH=${APP_SERVER_PATHS[$i]}
  APP_SERVER=${APP_SERVERS[$i]}

  # 启动程序
  echo "Starting app-2.1.7 server application $i..."
  nohup java -Xms512m -Xmx1024m -Xmn256m -Xss256k -jar "${APP_SERVER_PATH}${APP_SERVER}" --spring.profiles.active=prod > /dev/null 2>&1 &

  echo "app-2.1.7 server application $i restarted successfully."
done
